<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>抖动</title>
<style type="text/css">
img{position: absolute; top:200px;left: 100px;}
#img2{left: 220px;}
</style>
<script type="text/javascript">
window.onload = function(){
	var aImg = document.getElementsByTagName('img');
	for (var i=0;i<aImg.length;i++){
		aImg[i].onmouseover = function(){
			var _this = this;			//回调函数如果直接用this去指定，那么它指定的是window而不是当前图片，所以要先存起来
			shake(_this,'left',function(){
				shake(_this,'top');
			});	
		}
	}
}
//封装分割线
	function getStyle(obj,attr){
		return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj)[attr];
	}

	function shake(obj,attr,endFn){
		if (obj.flag) return;								//解决隐患
		obj.flag = true;

		var arr = [];
		var num = 0;
		var pos = parseInt(getStyle(obj,attr));				//隐患（一直在获取位置）

		for (var i=20;i>0;i-=2){
			arr.push(i,-i)
		}
		arr.push(0);

		clearInterval(obj.timer);
		obj.timer = setInterval(function(){
			obj.style[attr] = pos + arr[num] + 'px';
			num ++;
			if (num === arr.length) {
				clearInterval(obj.timer);
				endFn && endFn();
				obj.flag = false;
			};
		},50)	
	}
</script>
</head>
<body>
<img  id="img1" src="img/opera1.jpg">
<img  id="img2" src="img/opera2.jpg">
</body>
</html>